using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Comp477Project
{
   
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //===========class variables================
        Camera camera;
        Matrix projection;
        Hud hud;
        SpriteFont hudFont;
        Texture2D button;
        int winWidth, winHeight;
        Skybox skyBox;
        //ParticleSystem1 ps;
        Island island;
        Water water;
        EffectsHandler FX;
        public bool[] param;
        //========class variables=================
        //=====local variables=================
        bool FULL_SCREEN = false;
        //=====local variables==================

        //=====sounds======
        SoundEffect boom;
        Song bg;
        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            // Screen Setup
            if (FULL_SCREEN)
            {
                winWidth = 1920;
                winHeight = 1080;
                graphics.IsFullScreen = true;
            }
            else
            {
                winWidth = 800;
                winHeight = 600;
            }
            graphics.PreferredBackBufferWidth = winWidth;
            graphics.PreferredBackBufferHeight = winHeight;
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), (float)(winWidth) / (float)(winHeight), 0.001f, 500f);
        }

     
        protected override void Initialize()
        {
            camera = new Camera();
            this.IsMouseVisible = true;
            Window.Title = "COMP 477 - Project (Sugi, Mike, Bachir)";
            base.Initialize();
        }

        
        protected override void LoadContent()
        {
           
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            // boom
            boom = Content.Load<SoundEffect>(@"smcameron__volcano-eruption");

            // Effects Handler
            FX = new EffectsHandler(GraphicsDevice, ref projection,ref boom);

            // Load textures
            button = Content.Load<Texture2D>("button");

            // Load Font
            hudFont = Content.Load<SpriteFont>("HudFont");

            // Initialize Skybox
            skyBox = new Skybox("SkyBox/Sky", Content);

            // Initialize Hud
            hud = new Hud(ref button, winWidth, winHeight, hudFont);

            //test
            //ps = new ParticleSystem(GraphicsDevice, ref projection, 2);
            //ps.Reset(2);
            island = new Island(Content);
            water = new Water(Content);
            //======sounds====
            //boom=Content.Load<SoundEffect>(@"smcameron__volcano-eruption");
            bg=Content.Load<Song>(@"spring-weather-1");
            MediaPlayer.Play(bg);
            MediaPlayer.IsRepeating=true;
        }

       
        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
           
            //ps.Update(gameTime);
            camera.Update(Vector3.Zero);
            param= hud.Update();
            if (hud.waveUp)
            {
                water.update(gameTime, true,false);
                //Console.WriteLine("update");
                hud.waveUp = false;
            }
            else if(hud.waveD)
            {
                water.update(gameTime, false,true);
                hud.waveD = false;
            }
            
            else
            {
                water.update(gameTime, false,false);
            }

            FX.Update(gameTime,param);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // SKYBOX
            GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
            skyBox.Draw(camera.view, projection, new Vector3(0, 0, 0)/*camera.pos*/);
            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
            // Draw Elements
            //ps.Draw(camera.view);
            island.draw(camera.view, projection);
            water.DrawOcean(gameTime,GraphicsDevice,camera,projection);
            FX.Draw(camera.view,param);
            // Draw Hud
            hud.Draw(spriteBatch, ref camera, gameTime);
            // Fix Rendering back for 3D
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            
            base.Draw(gameTime);
        }
    }
}
